package gui.categories;

import javax.swing.DefaultListModel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import java.awt.Color;
import java.awt.GridBagLayout;
import javax.swing.JList;

import org.jdom.JDOMException;

import start.Start;

import xml.XMLRecipeReader;

import java.awt.GridBagConstraints;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

/**
 * Vorschau-Panel fuer Kategorieverwaltung.
 * Listet alle mit einer Kategorie verknuepften Rezepte auf.
 * @author Dennis Wittkoetter
 *
 */
public class CategoryPanel_Preview extends JPanel {
	/**ID zur Serialisierung*/
	private static final long serialVersionUID = 1L;
	/**XML-Reader zum Lesen von Rezepten*/
	private static XMLRecipeReader xmlreader;
	/**ScrollPane zur Auflistung verknuepfter Rezepte*/
	private static JScrollPane scrollPane;
	
	/**
	 * Konstruiert das Vorschau-Panel
	 * @param selRow	Aktuell in der CategoryTable selektierte Zeile
	 * @throws JDOMException
	 * @throws IOException
	 */
	public CategoryPanel_Preview(int selRow) throws JDOMException, IOException {
		xmlreader = new XMLRecipeReader(Start.getRezeptexml()); 
		JList catList = null;
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		if (selRow != -1) {	//wenn eine Zeile in der CategoryTable selektiert wurde
			List<String>recipeNames = xmlreader.getRecipesByCategory(CategoryPanel.getTable().getValueAt(selRow, 1).toString());
			Collections.sort(recipeNames);
			
			DefaultListModel model = new DefaultListModel();
			if (!recipeNames.isEmpty()) {
				for (String elem : recipeNames) 
					model.addElement(elem);
			}
			else
				model.addElement("keine verknüpften Rezepte");

			catList = new JList(model);
			
			if (recipeNames.isEmpty())	
				catList.setForeground(Color.RED);	//Schrift Rot
			else
				catList.setForeground(Color.BLACK);	//Schrift Schwarz
		
			scrollPane = new JScrollPane(catList);
			GridBagConstraints gbc_scrollPane = new GridBagConstraints();
			gbc_scrollPane.fill = GridBagConstraints.BOTH;
			gbc_scrollPane.gridx = 0;
			gbc_scrollPane.gridy = 0;
			add(scrollPane, gbc_scrollPane);
		}
	}
}
